#include <iostream>
#include <fstream>
#include <string>
#include <unistd.h>

#include "log.hpp"
using std::cin;
using std::cout;
using std::endl;
Log log;
bool isConnected(const std::string& interface) {
    std::string path = "/sys/class/net/" + interface + "/operstate";
    std::ifstream file(path);
    if (!file.is_open()) {
        log(WARNING,"open file err.:%s，%d",__FILE__,__LINE__);
        return false;
    }

    std::string state;
    file >> state;
    file.close();

    return state == "up";
}

std::string getSpeed(const std::string& interface) {
    std::string path = "/sys/class/net/" + interface + "/speed";
    std::ifstream file(path);
    if (!file.is_open()) {
        log(WARNING,"open file err.:%s，%d",__FILE__,__LINE__);
        return "未知";
    }

    int speed;
    file >> speed;
    file.close();
    if (speed == 1000) {
        return "千兆";
    } else if (speed == 100) {
        return "百兆";
    } else {
        log(INFO,"speed err...\n");
        return "其他速率";
    }
}

int main() {
    std::string inte="ens33";
    if (isConnected(inte)) {
        cout << inte << " 已连接。" << endl;
        string speed = getSpeed(inte);
        cout << "连接速率: " << speed << endl;
    } else {
        cout << "网卡 " << inte << " 未连接。" << endl;
    }

    return 0;
}